// pages/invitation/invitation.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[
      // {id:1,nickname:'发发发发',count:2},
      // {id:4,nickname:'发发fafa发发',count:42},
      // {id:3,nickname:'发faf发发发',count:12},
    ],
    firstSum:0,
    secondSum:0,
    scrollindex:0, // 邀请级别
    hidden: true,
    loadingData:false,
    meid:'',
    offset:0, //偏移
    listSecond:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  sortNum(attr,rev){
    if (rev == undefined) {
      rev = 1;
    } else {
      rev = (rev) ? 1 : -1;
    }
    return function (a, b) {
      a = a[attr];
      b = b[attr];
      if (a < b) {
        return rev * -1;
      }
      if (a > b) {
        return rev * 1;
      }
      return 0;
    }
  },
  first(json,data){ //一级
    let that = this;
    getApp().ports.api36({json,data}).then((res) => {
      let datalist = res.data.results
      if (that.data.Loading != '') {
        wx.hideLoading({
          success: (res) => {},
        })
      }
      if (datalist) {
        for (var j = 0; j < datalist.length; j++) {that.data.list.push(datalist[j])}
        datalist = that.data.list
      }
      that.setData({ listTotal : res.data.total })
      that.setData({ list:datalist })
      // let telphone = res.data.res
      that.setData({ firstSum: res.data.total})
      // that.queryTelphone(1); //批量获取用户联系方式
      // that.queryCount(1); //批量获取用户邀请数量
    }).catch((error)=>{console.log('我的邀请获取用户列表出错了哦！',error)})
  },
  second(json,datasecond){
    let that = this;
    let data = datasecond;
    getApp().ports.api36({json,data}).then((res) => {
      let datalist = res.data.results
      if (that.data.Loading != '') {
        wx.hideLoading({
          success: (res) => {},
        })
      }
      if (datalist) {
        datalist.forEach(item => {
          that.data.listSecond.push(item)
        })
        datalist = that.data.listSecond
      }
      that.setData({ listSecondTotal : res.data.total })
      that.setData({ listSecond:datalist })
      that.setData({ secondSum:res.data.total })
      // that.queryTelphone(2); //批量获取用户联系方式
      // that.queryUrl(datalist); //批量获取用户邀请路径
      // that.queryCount(2); //批量获取用户邀请数量
    }).catch((error)=>{console.log('我的邀请获取用户列表出错了哦！',error)})
  },
  scroll(e){ //左右滑动
    // console.log(e.detail.name)
    let that = this;
    that.setData({offset:0})
    that.setData({ scrollindex : e.detail.name })
    if(e.detail.name == 0){
      that.setData({ list: [] })
      let data = {depth:1,page_size:20,page_token:that.data.offset}
      let json = {user_pk:that.data.meid}
      if(!that.data.list.length){
        that.first(json,data)
      }
    }else if(e.detail.name == 1){
      that.setData({ listSecond: [] })
      let datasecond = {depth:2,page_size:20,page_token:that.data.offset}
      let json = {user_pk:that.data.meid}
      if(!that.data.listSecond.length){
        that.second(json,datasecond)
      }
    }
  },
  scrollToLower: function(e) { //上拉加载
    let that = this;
    if(that.data.scrollindex == 0){
      let offset = that.data.offset+=1
      let data = {depth:1,page_size:20,page_token:offset}
      let json = {user_pk:that.data.meid}
      let total_size = that.data.listTotal; //数据总数
      if(that.data.list.length < total_size){
        that.data.Loading = wx.showLoading({ title: '加载中',  })
        that.first(json,data)
      }
    }else if(that.data.scrollindex == 1){
      let offset = that.data.offset+=1
      let data = {depth:2,page_size:20,page_token:offset}
      let json = {user_pk:that.data.meid}
      let total_size = that.data.listSecondTotal; //数据总数
      if(that.data.listSecond.length < total_size){
        that.data.Loading = wx.showLoading({ title: '加载中',})
        that.second(json,data)
      }
    }
    // console.log(total_size)
  },
  onLoad: function (options) {
    var that = this;
    wx.hideShareMenu({
      menus: ['shareAppMessage', 'shareTimeline']
    })
    getApp().ports.api30({}).then((res) => {
      that.setData({ meid:res.data.id })
      let data = {depth:1,page_size:20,page_token:that.data.offset} //一级邀请
      let datas = {depth:2,page_size:20,page_token:that.data.offset} //二级邀请
      let json = {user_pk:res.data.id}
      that.first(json,data)
      that.querysecond(json,datas);
    })
  },
  querysecond(json,datas){ //页面加载获取二级邀请总数
    let that = this;
    let data = datas;
    getApp().ports.api36({json,data}).then((res) => {
      that.setData({ secondSum : res.data.total })
    })
  },
  queryTelphone(num){
    var that = this;
    let reg = /^(\d{3})\d{4}(\d{4})$/; //隐藏中间四位数
    if(num == 1){
      let arr = [];
      for (let i in that.data.list) {arr.push(that.data.list[i].id);}
      getApp().ports.api38({method:'POST',data:arr}).then((res) => {
        // console.log(res)
        for (let i in res.data) {
          for(let j in that.data.list){
            if(that.data.list[j].id==res.data[i].id){ //判断id是否一致，一致时将获取到的联系电话加到数据
              if(res.data[i].phone) that.data.list[j].tel = res.data[i].phone.replace(reg,"$1****$2") //把拿去到的联系电话，传到数据中
            }
          }
        }
      }).catch((error)=>{ console.log('批量获取用户电话出错了！',error) })
    }
    if(num == 2){
      let arr2 = [];
      for (let i in that.data.listSecond) {arr2.push(that.data.listSecond[i].id);}
      getApp().ports.api38({data:arr2}).then((res) => {
        // console.log(res.data)
        for (let i in res.data) {
          for(let j in that.data.listSecond){
            if(that.data.listSecond[j].id==res.data[i].id){ //判断id是否一致，一致时将获取到的联系电话加到数据
              // console.log(res.data[i])
              if(res.data[i].phone) that.data.listSecond[j].tel = res.data[i].phone.replace(reg,"$1****$2")  //把拿去到的联系电话，传到数据中
            }
          }
        }
      }).catch((error)=>{ console.log('批量获取用户电话出错了！',error) })
    }
  },
  queryCount(num){
    var that = this;
    var list = that.data.list;
    var listSecond = that.data.listSecond;
    if(num == 1){
      let arr = [];
      for (let i in that.data.list) {arr.push(that.data.list[i].id); }
      getApp().ports.api39({data:arr}).then((res) => {
        for (let i = 0; i< res.data.length;i++) {
          let kk = res.data[i].filter(p=>{ return p.depth == 1 }) //拿到该用户的一级邀请
          if(JSON.stringify(kk) != '[]'){
            that.data.list[i].count = kk[0].count ; //把拿去到数量，传到数据中
          }else{
            that.data.list[i].count = 0
          }
        }
        if(that.data.list) that.data.list.sort(that.sortNum('count',false)) //降序
        that.setData({ list:list })
      }).catch((error)=>{ console.log('批量获取用户数量出错了！',error) })
    }
    if(num == 2){
    let arr2 = [];
    for (let i in that.data.listSecond) { arr2.push(that.data.listSecond[i].id);}
    getApp().ports.api39({data:arr2}).then((res) => { //二级邀请
      for (let i = 0; i< res.data.length;i++) {
        let kk = res.data[i].filter(p=>{ return p.depth == 1 }) //拿到该用户的一级邀请
        if(JSON.stringify(kk) != '[]'){
          that.data.listSecond[i].count = kk[0].count ; //把拿去到数量，传到数据中
        }else{
          that.data.listSecond[i].count = 0
        }
      }
      if(that.data.listSecond) that.data.listSecond.sort(that.sortNum('count',false)) //降序
      that.setData({listSecond:listSecond})
    }).catch((error)=>{ console.log('批量获取用户数量出错了！',error)})
  }
  },
  sum(arr){ //总数
    var s=0;
    arr.forEach(function(val,idx,arr){  s+=val; });
    return s;
  },
  queryUrl(datalist){
    var that = this;
    let arr = [];
    for (let i in datalist) { arr.push(datalist[i].id);}
    getApp().ports.api40({data:arr}).then((res) => {
      console.log(res.data)
      for(var j in that.data.list){
        for(var i in res.data){
          if(res.data[i][1]==that.data.list[j].id){
            datalist[i].inviUser = that.data.list[j].nickname;
          }
        }
      }
    }).catch((error)=>{
      console.log('批量获取用户地址出错了！',error)
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  
})